package com.cm.task;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.connection.StringRedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.nodes.CollectionNode;

import java.util.*;

/**
 * @author chenming
 * @description
 * @create: 2022-07-19
 */
@EnableScheduling
@Component
@Slf4j
public class Task {

    @Autowired
    private StringRedisTemplate redis1StringRedisTemplate;

    @Autowired
    private StringRedisTemplate redis2StringRedisTemplate;

//    @Scheduled(cron = "0/1 * * * * ?")
    public void test() {
        Set<String> s = redis2StringRedisTemplate.keys("card:*");
        List<String> keys = new ArrayList<String>(s);

        List<String> values = redis2StringRedisTemplate.opsForValue().multiGet(keys);

        keys.removeAll(Collections.singletonList(null));
        values.removeAll(Collections.singletonList(null));

        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < keys.size(); i++) {
            map.put(keys.get(i), values.get(i));
        }

        batchSetOrExpire(map, 10L, redis1StringRedisTemplate);

    }

    public void batchSetOrExpire(Map<String, String> map, Long seconds, StringRedisTemplate stringRedisTemplate) {
        RedisSerializer<String> serializer = stringRedisTemplate.getStringSerializer();
        stringRedisTemplate.executePipelined(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                map.forEach((key, value) -> {
//                    connection.set(serializer.serialize(key), serializer.serialize(value), Expiration.seconds(seconds), RedisStringCommands.SetOption.UPSERT);
                    connection.set(serializer.serialize(key), serializer.serialize(value));
                });
                return null;
            }
        }, serializer);
    }

    /**
     * 批量查询，管道pipeline
     *
     * @param keys
     * @return
     */
    public List<Object> batchGet(List<String> keys, StringRedisTemplate stringRedisTemplate) {
        List<Object> result = stringRedisTemplate.executePipelined(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                StringRedisConnection src = (StringRedisConnection) connection;

                for (String k : keys) {
                    src.get(k);
                }
                return null;
            }
        });

        return result;
    }

}
